﻿using System;
using System.Web;
using FrienDev.BusinessRules;
using WindowsLive;

/// <summary>
/// This page handles the login, logout and clearcookie Web Auth
/// actions.  When you create a Windows Live application, you must
/// specify the URL of this handler page.
/// </summary>
public partial class HandlerPage : System.Web.UI.Page
{
    private static DateTime m_ExpireCookie = DateTime.Now.AddYears(-10);
    private static DateTime m_PersistCookie = DateTime.Now.AddYears(10);

    protected void Page_Load(object sender, EventArgs e)
    {
        HttpRequest req = HttpContext.Current.Request;
        HttpResponse res = HttpContext.Current.Response;

        // Extract the 'action' parameter from the request, if any.
        string action = req["action"];

        /*
          If action is 'logout', clear the login cookie and redirect
          to the logout page.

          If action is 'clearcookie', clear the login cookie and
          return a GIF as response to signify success.

          By default, try to process a login. If login was
          successful, cache the user token in a cookie and redirect
          to the site's main page.  If login failed, clear the cookie
          and redirect to the main page.
        */

        if (action == "logout")
        {
            try
            {
                OnlineRules.Instance.UserLogout(UserRules.Instance.GetCurrentUserId());
            }
            catch
            {
            }
            HttpCookie loginCookie = new HttpCookie(ApplicationSettings.LOGINCOOKIE);
            loginCookie.Expires = m_ExpireCookie;
            res.Cookies.Add(loginCookie);
            res.Redirect(ApplicationSettings.PUBLICDEFAULTFILENAME);
            res.End();

            Session.Abandon();
        }
        else if (action == "clearcookie")
        {
            HttpCookie loginCookie = new HttpCookie(ApplicationSettings.LOGINCOOKIE);
            loginCookie.Expires = m_ExpireCookie;
            res.Cookies.Add(loginCookie);

            string type;
            byte[] content;
            ApplicationSettings.WindowsLiveLogin.GetClearCookieResponse(out type, out content);
            res.ContentType = type;
            res.OutputStream.Write(content, 0, content.Length);

            res.End();
        }
        else
        {
            WindowsLiveLogin.User user = ApplicationSettings.WindowsLiveLogin.ProcessLogin(req.Form);

            HttpCookie loginCookie = new HttpCookie(ApplicationSettings.LOGINCOOKIE);
            if (user != null)
            {
                loginCookie.Value = user.Token;

                if (user.UsePersistentCookie)
                {
                    loginCookie.Expires = m_PersistCookie;
                }
            }
            else
            {
                loginCookie.Expires = m_ExpireCookie;
            }

            res.Cookies.Add(loginCookie);
            OnlineRules.Instance.UserLogin(user.Id);
            OnlineRules.Instance.BecomeFriend();
            OnlineRules.Instance.ViewActivity();
            res.Redirect(ApplicationSettings.HOMEPAGEFILENAME);
            res.End();
        }
    }
}